package com.example.user.mapper;

import com.example.user.entity.User;
import com.example.utils.mybatis.SimpleInsertLanguageDriver;
import com.example.utils.mybatis.SimpleSelectInLanguageDriver;
import com.example.utils.mybatis.SimpleSelectLanguageDriver;
import com.example.utils.mybatis.SimpleUpdateLanguageDriver;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Lang(SimpleInsertLanguageDriver.class)
    @Insert("INSERT INTO user (#{user})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 获取数据库自增id
    Long insert(User user);

    /**
     * 查找全部
     */
    @Select("SELECT * FROM user")
    @ResultMap("com.example.user.mapper.UserMapper.BaseResultMap")
    List<User> getAll();

    /**
     * 根据id找对象
     */
    @Select("SELECT * FROM user WHERE id = #{id}")
    @ResultMap("com.example.user.mapper.UserMapper.BaseResultMap")
    User findById(Long id);

    /**
     * 根据id列表，批量查询用户
     */
    @Lang(SimpleSelectInLanguageDriver.class)
    @Select("SELECT * FROM user WHERE id IN (#{ids})")
    @ResultMap("com.example.user.mapper.UserMapper.BaseResultMap")
    List<User> findByIds(@Param("ids") List<Long> ids);

    /**
     * 根据条件，批量查询列表
     */
    @Lang(SimpleSelectLanguageDriver.class)
    @Select("SELECT * FROM user (#{user})")
    @ResultMap("com.example.user.mapper.UserMapper.BaseResultMap")
    List<User> findListByCondition(User user);

    /**
     * 更新对象
     */
    @Lang(SimpleUpdateLanguageDriver.class)
    @Update("UPDATE user (#{user}) WHERE id = #{id}")
    Integer updateById(User user);

    /**
     * 根据id删除用户
     */
    @Delete("DELETE FROM user WHERE id = #{userId}")
    Integer deleteById(Long id);

    /**
     * 批量删除用户
     */
    @Lang(SimpleSelectInLanguageDriver.class)
    @Delete("DELETE FROM user WHERE id IN (#{ids})")
    Integer deleteByIds(@Param("ids") List<Long> ids);

}